package com.example.lesson1;


import java.util.concurrent.TimeUnit;

import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import com.opera.core.systems.OperaDriver;

import static org.junit.Assert.fail;



public class TC001LoginLogout {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
    }



    @Test
    public void LoginLogout() throws Exception {
        String[] browsers = {"chrome", "ff", "opera", "ie"};
        for (int i = 0; i <= browsers.length; i++) {
                driver = getWebDriver(browsers[i]);
                goToMainPage();
                loginAs("admin", "admin");
                logout();
        }
    }

    @Test
    public void CreateNewUser() throws Exception {
        goToMainPage();
        loginAs("admin", "admin");
        goToUserManagementPage();
        createNewUser("test user", "testuser@test.com", "test11", "test11");
        logout();
    }

    @Test
    public void UpdateUser() throws Exception {
        goToMainPage();
        loginAs("admin", "admin");
        goToUserManagementPage();
        findUser();
        updateUserPassword("q1w2", "q1w2");
        logout();
    }

    @Test
    public void DeleteUser() throws Exception{
        goToMainPage();
        loginAs("admin", "admin");
        goToUserManagementPage();
        deleteUser();
        logout();
    }

    private void deleteUser() {
        driver.findElement(By.xpath("(//img[@alt='Remove'])")).click();
        driver.switchTo().alert().accept();
    }

    private void findUser() {
        driver.findElement(By.linkText("test user")).click();
    }

    private void updateUserPassword(String password, String password1) {
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("password2")).clear();
        driver.findElement(By.id("password2")).sendKeys(password1);
        driver.findElement(By.name("submit")).click();
    }

    private void goToUserManagementPage() {
        driver.findElement(By.linkText("User management")).click();
    }

    private void createNewUser(String username, String email, String password, String password1) {
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys(email);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("password2")).clear();
        driver.findElement(By.id("password2")).sendKeys(password1);
        driver.findElement(By.name("submit")).click();
    }


    private void logout() {
        driver.findElement(By.name("submit")).click();
        driver.findElement(By.linkText("Log out")).click();
        driver.switchTo().alert().accept();
        driver.quit();
    }

    private void loginAs(String username, String password) {
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys(password);
    }

    private void goToMainPage() {
        driver.get(baseUrl + "/php4dvd/");
    }

    public WebDriver getWebDriver(String browser) {
        if (browser.equals("ff")) {
            driver = new FirefoxDriver();

        } else if (browser.equals("ie")) {
            System.setProperty("webdriver.ie.driver", "E:\\Selenium Training\\Browsers Drivers\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();

        } else if (browser.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", "E:\\Selenium Training\\Browsers Drivers\\chromedriver.exe");
            driver = new ChromeDriver();

        } else if (browser.equals("opera")) {
            driver = new OperaDriver();

        } else {
            driver = new FirefoxDriver();
        }

        baseUrl = "http://localhost/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        return driver;
    }


    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}
